﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace LBCMS.Helper
{
    public class FileHelper
    {

        /// <summary>
        /// 读取文件所有内容
        /// 刘军 2017-5-15 14:51:42 rainy
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ReadFile(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();
            StreamReader sr = new StreamReader(FilePath, Encoding.UTF8);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                sb.Append(line.ToString());
            }
            sr.Close();
            return sb.ToString();

        }

        public List<fileInfo> getFileByFileType(string path, string fileType)
        {
            List<fileInfo> files = new List<fileInfo>();
            DirectoryInfo dir = new DirectoryInfo(path);
            //path为某个目录，如： “D:\Program Files”
            FileInfo[] inf = dir.GetFiles();
            foreach (FileInfo finf in inf)
            {
                if (finf.Extension.Equals("." + fileType))
                {
                    files.Add(new fileInfo() { Name = finf.Name, FullName = finf.FullName });
                }

            }

            return files;
        }

        public class fileInfo
        {
            public string Name { get; set; }
            public string FullName { get; set; }
        }

        public void WriteToFile(string path, string content)
        {


            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            //开始写入
            sw.Write(content);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();

        }
    }
}
